<?php

namespace App\Http\Controllers;

use App\Constants\RedisKey;
use App\Events\RiskResultEvent;
use App\Facades\CommonPlatformRequestFacade;
use App\Facades\MoneyPlatformRequestFacade;
use App\Library\Guzzle\MoneyPlatformRequest;
use App\Library\Tools\ExcelExport;
use App\Models\AdminBankIcon;
use App\Models\LoanApply;
use App\Models\RepayRecord;
use App\Models\RepaySummary;
use App\Models\Traits\RepaySummaryInteface;
use App\Models\UserApplyRiskLog;
use App\Models\UserAuthSpiderTransLog;
use App\Models\UserBankCard;
use App\Models\UserGlobal;
use App\Models\UserLoanRequest;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class TestController extends Controller
{

    public function test()
    {
        dd(2);
    }

    public function test1111(Request $request)
    {

    }

    public function testtest()
    {
        $res =  Redis::get('key');
        dd($res);
    }

    public function destroy()
    {
        return 'delete';
    }

    public function update()
    {
        return 'update';
    }

    public function asyncSpider($data){
        foreach ($data as $v){
            dd($data);
            if($v['phone_trade_no']){
                $model = new UserAuthSpiderTransLog();
                $model->type = 'carrier';
                $model->status = $v['phone_status'];
                $model->loan_request_id = $v['loan_request_id'];
                $model->user_id = $v['user_id'];
                $model->trade_no = $v['phone_trade_no'];
                $model->failed_reason = $v['phone_failed_reason'];
                $model->save();
            }
            if($v['zhima_trade_no']){
                $model = new UserAuthSpiderTransLog();
                $model->type = 'alipay';
                $model->status = $v['zhima_status'];
                $model->loan_request_id = $v['loan_request_id'];
                $model->user_id = $v['user_id'];
                $model->trade_no = $v['zhima_trade_no'];
                $model->failed_reason = $v['phone_failed_reason'];
                $model->save();
            }
            unset($model);
        }
    }

    public function export_1(Request $request)
    {
        ini_set('memory_limit','1024M');
        $query = LoanApply::from('loan_apply as apply')
            ->select([
                's.id as repay_id',
                'u.id as user_id',                                                            //用户id
                'request.credit_id',                                                          //授信id
                'u.nfsp_user_id',                                                            //资方用户id
                'u.phone_num',                                                               //用户电话
                'apply.out_trade_id',                                                        //借款订单号
                'request.channel',                                                           // 客户渠道
                'apply.loan_id',                                                             // 放款交易号
                'apply.amount',                                                             // 放款金额
                'apply.period',                                                             // 分期数
                'apply.pre_insurance_amt',
                'apply_time',         // 放款申请时间
                'loan_time',      // 实际放款时间
            ])
            ->with(['getUser.valid', 'getSummary.recordMany', 'getSummary.money'])
            ->leftJoin('user_loan_requests as request', 'request.id', '=', 'apply.loan_request_id')
            ->leftJoin('users as u', 'u.id', '=', 'apply.user_id')
            ->leftJoin(RepaySummary::getModel()->getTable() . ' as s', 's.loan_id', '=', 'apply.loan_id')
            ->whereNotNull('apply.loan_time');
        $data = [];
        foreach ($query->cursor() as $item) {
            $item->channel_name = $item->channel == 'JCFC' ? '晋商' : '光大';
            $item->apply_time = date('Y/m/d H:i:s',strtotime($item->apply_time));
            $item->loan_time = date('Y年m月d日 H:i:s',strtotime($item->loan_time)).' '.getDayWithCh($item->loan_time);
            $item->channle_num = '';
            $item->channel_source = '';
            $item->product_name = '快闪卡贷';

            $item->platform_service = RepaySummary::getOrderTotalMoney($item->getSummary, 0, 'manage');
            $item->platform_other = '';
            $item->apply_num =1;
            $item->name = $item->getUser->valid->name;
            $item->cert_num = $item->getUser->valid->num;
            $period = 1;
            $amtTotal = 0;
            $amountTotal = 0;

            foreach ($item->getSummary->money as $key => $item1) {

                $propertyAmount = 'period_' . $period . '_amount';
                $propertyDay = 'period_' . $period . '_day';
                $propertyAmt = 'period_' . $period . '_amt';
                $item->$propertyAmt = $item1->lending_money;
                $propertyService = 'period_' . $period . '_service';
                $propertyOther = 'period_' . $period . '_other';
                $item->$propertyService = '';
                $item->$propertyOther = '';
                foreach ($item->getSummary->recordMany as $item2) {
                    if ($item2->id == $item1->record_id) {
                        $item->$propertyAmount = $item2->amount;
                        $item->$propertyDay = $item2->normal_date;
                        $item->$propertyService = RepaySummary::getOrderTotalMoney($item->getSummary, $period, 'manage');
                        break;
                    }
                }
                $amtTotal = bcadd($item->$propertyAmt, $amtTotal, 2);
                $amountTotal = bcadd($item->$propertyAmount, $amountTotal, 2);
                $propertyTotal = 'period_' . $period . '_total';
                $item->$propertyTotal = bcadd($item->$propertyAmt, $item->$propertyAmount, 2);

                $period++;
            }
            if ($period < 12) {
                while ($period <= 12) {
                    $propertyAmount = 'period_' . $period . '_amount';
                    $propertyDay = 'period_' . $period . '_day';
                    $propertyAmt = 'period_' . $period . '_amt';
                    $propertyTotal = 'period_' . $period . '_total';
                    $propertyService = 'period_' . $period . '_service';
                    $propertyOther = 'period_' . $period . '_other';
                    $item->$propertyTotal = '0.00';
                    $item->$propertyAmt = "0.00";
                    $item->$propertyAmount = "0.00";
                    $item->$propertyDay = '';
                    $item->$propertyService = '';
                    $item->$propertyOther = '';
                    $period++;
                }
            }

            $item->amount_total = $item->amount;
            $item->amt_total = $amtTotal;
            $item->all_total = RepaySummary::getOrderTotalMoney($item->getSummary);
            unset($item->getUser, $item->getSummary);
            $data[] = $item->toArray();
        }


        $header = [
            '授信ID',
            '产品',
            '用户号',
            '姓名',
            '手机号',
            '身份证',
            '客户渠道号',
            '客户渠道来源',
            '放款交易号',
            '放款资方渠道',
            '放款金额',
            '分期',
            '申请放款时间',
            '笔数',
            '实际放款时间',
            '应收总金额',
            '应收总本金',
            '应收总利息',
            '应收平台总服务费',
            '应收平台其他费用',
            '保险费用',
        ];
        $fields = [
            'credit_id',
            'product_name',
            'nfsp_user_id',
            'name',
            'phone_num',
            'cert_num',
            'channel',
            'channel_source',
            'loan_id',
            'channel_name',
            'amount',
            'period',
            'apply_time',
            'apply_num',
            'loan_time',
            'all_total',
            'amount_total',
            'amt_total',
            'platform_service',
            'platform_other',
            'pre_insurance_amt',
        ];
        $i=1;
        while($i <= 12){
            $header[] = '还款日期-'.$i.'期';
            $fields[] = 'period_'.$i.'_day';
            $header[] =  '还款总金额-'.$i.'期';
            $fields[] = 'period_'.$i.'_total';
            $header[] = '还款本金-'.$i.'期';
            $fields[] = 'period_'.$i.'_amount';
            $header[] = '还款利息-'.$i.'期';
            $fields[] = 'period_'.$i.'_amt';
            $header[] = '还款服务费-'.$i.'期';
            $fields[] = 'period_'.$i.'_service';
            $header[] = '还款其他费用-'.$i.'期';
            $fields[] = 'period_'.$i.'_other';
            $i++;
        }
        $excel = new  ExcelExport($data, $header, $fields);
        $excel->export();
    }

//    public function testtest(Request $request)
//    {
//        $user = $request->user();
//        $user->canLoan();
//        foreach(User::where('id','>',0)->cursor() as $item){
//           $item->phone_num_sha = hash('SHA256',$item->phone_num);
//           $item->save();
//        }
//       echo 'success';
//    }
//
//    public function destroy()
//    {
//        return 'delete';
//    }
//
//    public function update()
//    {
//        return 'update';
//    }
    public function signData()
    {
        return [
            13857196332,
            13465102569,
            18285179467,
            15826535999,
            18787490630,
            13876049460,
            15942150606,
            18342822969,
            15138678673,
            15021286330,
            13795277710,
            13619175812,
            18622680596,
            15292252552,
            13602361665,
            18675910325,
            13063866382,
            13971608658,
            18273160061,
            18591960795,
            13545192291,
            18777943979,
            13832589003,
            18049809083,
            13807047979,
            13993632742,
            13338923666,
            15842453386,
            18228611652,
            18633324458,
            18636305331,
            18763039954,
            18081388886,
            15262144970,
            15937309192,
            15921508350,
            15880290185,
            17755579127,
            18502997970,
            17610097333,
            13986006969,
            15295760949,
            13568851742,
            15662009123,
            15712876245,
            13675215207,
            13723701814,
            15951157527,
            15577728290,
            18035970006,
            13854765288,
            15161609360,
            18721621800,
            15803226764,
            15960835048,
            15926323316,
            13502623548,
            13817715878,
            13520413227,
            13638606911,
            18624063337,
            13706633899,
            15800241676,
            13994657393,
            13750577725,
            18580006722,
            15105847778,
            16602872066,
            15270827559,
            15757777508,
            15926794108,
            18156577799,
            13169878635,
            17366926666,
            13323857299,
            17739467718,
            15902720606,
            13866766977,
            18629752018,
            13138984332,
            15211286610,
            13764958930,
            14704661664,
            18907988217,
            18241927707,
            13778328488,
            13648808543,
            15061768575,
            15820722377,
            18764017107,
            18650808099,
            18945677087,
            13375411033,
            18390865289,
            18288853060,
            13906067825,
            13951860856,
            15015172104,
            15189107168,
            18383243526,
            13854969311,
            15085973039,
            18856637797,
            13067985352,
            15057134895,
            15590475522,
            13755342734,
            13975950851,
            13913200690,
            15232955512,
            13799990537,
            18919286080,
            18806647906,
            18810907747,
            13904866888,
            13569002644,
            13023926333,
            18271256366,
            15771019886,
            15111878828,
            15902887262,
            13539080111,
            13832352016,
            13915868884,
            13689658852,
            13959759191,
            15841304647,
            13551011065,
            18685112603,
            15576660678,
            13946002099,
            18760575780,
            13903383970,
            18802703113,
            18895059606,
            13505465614,
            18863228224,
            18744044349,
            15990308382,
            18981686607,
            18552899797,
            15989914015,
            15156040644,
            15500111881,
            18776703766,
            13359521884,
            15578962088,
            17636336850,
            13652833399,
            15007835999,
            18319951236,
            13673139051,
            18253568112,
            15873971119,
            13396226155,
            18601616472,
            18990605965,
            13530330502,
            13670837829,
            18858789551,
            18302465406,
            15227272044,
            13971476600,
            15562225612,
            13619414986,
            18575389019,
            13925144468,
            15860873061,
            18220710111,
            18200291567,
            18813515234,
            15738926190,
            15011558050,
            13433484287,
            18346178260,
            18857929686,
            15946345554,
            13994806425,
            15868382205,
            18050733117,
            18259253385,
            13301418787,
            18777381005,
            18925812251,
            13817678947,
            13795957268,
            18683082979,
            18622307511,
            18266235166,
            15906009668,
            15186027768,
            15054776511,
            18788546095,
            13859207561,
            13325192577,
            15158912747,
            18857717377,
            18217636562,
            18932228300,
            15651302996,
            13862298624,
            13589622254,
            13729061661,
            18608022291,
            13738970383,
            15958988108,
            13916608258,
            15234105556,
            13865978959,
            18287354056,
            18688991819,
            18712313315,
            18251015986,
            18788705711,
            18677025631,
            15860445547,
            18553573315,
            15821476771,
            15213183689,
            18627771347,
            13750767885,
            13568871321,
            13705492533,
            18132220880,
            15271693534,
            15159230117,
            13377470966,
            15910106423,
            13605543610,
            15552883657,
            15362090118,
            18521300212,
            13413930282,
            13957835783,
            13728464398,
            18004567158,
            18663448022,
            15969208757,
            15828230523,
            13615511655,
            18627451322,
            18372662920,
            13862791105,
            18629600857,
            13479933045,
            18666902784,
            13526226223,
            17732131528,
            13643376747,
            13839220536,
            15821764168,
            13877565754,
            18094050678,
            13556701456,
            18502136855,
            15912398979,
            13135852345,
            13735308875,
            13323989199,
            13517602930,
            18011858971,
            17640205759,
            18915122008,
            13883536695,
            15557980555,
            13616041033,
            15921975618,
            15052038380,
            13985340632,
            13959920575,
            15815702401,
            13419644244,
            13701077501,
            13924649351,
            15039078568,
            18925202876,
            18119505234,
            17673046726,
            13767646466,
            17779712003,
            15872411353,
            13724863206,
            13408063086,
            13990213606,
            13418619722,
            15872581231,
            15910082573,
            13450458658,
            18121153891,
            15066096516,
            15169429960,
            18956344796,
            13407100206,
            13736022595,
            15287167717,
            15248077868,
            13725570119,
            15049975315,
            18681870609,
            13636216000,
            15964083598,
            15889320681,
            13546161428,
            13108859502,
            18796019390,
            13339776042,
            13583120138,
            18377393083,
            18263153195,
            13695480009,
            13410127518,
            18734803443,
            18224088872,
            13786194125,
            18820929202,
            15013589300,
            18620099697,
            18902222522,
            15833528290,
            15036953972,
            13292132111,
            15616336133,
            13607711920,
            18122194842,
            13418897779,
            15953595965,
            13470017658,
            15003395385,
            13828676771,
            17705121031,
            15913800769,
            15657939348,
            15641865700,
            13995953083,
            15550708627,
            18228242281,
            18795394566,
            15011995562,
            15225911003,
            13895789909,
            15802930332,
            15837592252,
            18764619222,
            13591676106,
            15989231701,
            18682422650,
            13787688806,
            18772757983,
            15135391117,
            13144460555,
            15892644619,
            18985192073,
            15909609043,
            18736450328,
            13450225953,
            18368643444,
            13506111488,
            15029350280,
            13567032351,
            13827379019,
            15918585424,
            15062405787,
            13547869101,
            18907350635,
            13915477910,
            18601077283,
            18521569300,
            15051642970,
            15019621208,
            13877518132,
            15895218200,
            18588446635,
            13876953995,
            15030148816,
            13760048292,
            15967886553,
            13737033612,
            13910996897,
            13381617979,
            18221573791,
            13599548298,
            18676195354,
            18823176015,
            15089986297,
            13813289225,
            18733457654,
            13776600931,
            13210659729,
            13297067933,
            15137125559,
            15088131349,
            15049264002,
            15286320500,
            18679102121,
            13486546965,
            18600566522,
            13876042296,
            18858503381,
            15090791353,
            18607717894,
            18981135125,
            15120968319,
            13605656123,
            18136722799,
            15101663827,
            13814917751,
            15998434840,
            13846719717,
            13798856049,
            13689288942,
            15097559393,
            18048711785,
            13713389358,
            15833097405,
            13009021166,
            13642920612,
            15387099370,
            13983634062,
            15927493011,
            18903472209,
            15709693053,
            15035599292,
            13669001501,
            15210152838,
            13802838367,
            15940957879,
            18850931727,
            18897484655,
            13722857150,
            13819871511,
            13959679880,
            15223004367,
            15221445513,
            15234665379,
            13691787695,
            13925625822,
            13750477728,
            18669718155,
            15045487234,
            13868160520,
            18971056430,
            18913785369,
            15906724905
        ];
    }

}

